﻿namespace ELF;

public static class DefaultCorsEx
{
    public static void AddDefaultCors(this IHostApplicationBuilder builder)
    {
        var origins = builder.Configuration.GetSection("CorsOrigins").Get<string[]>();
        if (origins is null || origins.Length == 0)
            return;

        // Add CORS policy (only if origins are configured)
        builder.Services.AddCors(options =>
        {
            options.AddDefaultPolicy(
                policy =>
                {
                    policy.AllowAnyHeader().AllowAnyMethod().AllowCredentials().WithOrigins(origins);
                });
        });
    }
    public static IApplicationBuilder UseDefaultCors(this IApplicationBuilder app)
    {
        var config = app.ApplicationServices.GetRequiredService<IConfiguration>();
        var origins = config.GetSection("CorsOrigins").Get<string[]>();
        if (origins is null || origins.Length == 0)
            return app;
        return app.UseCors();
    }
}
